package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.BossIndustryDao;
import com.qc.entity.BossIndustryEntity;
import com.qc.entity.BossJobEntity;

@Repository("bossIndustryDao")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class BossIndustryDaoImpl implements BossIndustryDao{

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public int getListBossIndustryCount(String name) {

        String sql = "select count(*) from t_boss_industry where 1 = 1  and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public List<BossIndustryEntity> getAllBossIndustry(String name, int pageNo,int pageSize) {

        int beginRow = (pageNo - 1)* pageSize;
        
        String sql = "select industry_id,name from t_boss_industry where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BossIndustryEntity.class));
    }

    @Override
    public BossIndustryEntity getBossIndustryId(String industryId) {

        String sql = "select industry_id,name from t_boss_industry where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(industryId) && !"undefined".equals(industryId) ) {
            sql += " and industry_id = '"+industryId+"' ";
        }
        
        return (BossIndustryEntity) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(BossIndustryEntity.class));
    }

    @Override
    public int saveBossIndustry(BossIndustryEntity bossIndustry) {
        
        String sql = "insert into t_boss_industry(name) values(?)";
        return jdbcTemplate.update(sql,new Object[]{bossIndustry.getName()});
    }

    @Override
    public int deleteBossIndustryId(long id) {

        String sql = "update t_boss_industry set if_del = '1' where industry_id = " +id;
        return jdbcTemplate.update(sql);
    }

    @Override
    public int updateBossIndustryId(BossIndustryEntity bossIndustry) {

        String sql = "update t_boss_industry set name = ? where industry_id = ? ";
        return jdbcTemplate.update(sql,new Object[]{bossIndustry.getName(),bossIndustry.getIndustryId()});
    }

	@Override
	public List<BossIndustryEntity> getAllBossIndustry() {
		
		String sql = "select * from qc.t_boss_industry where if_del = 0";
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BossIndustryEntity.class));
	}

}
